﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Win104.IEC104
{
    /// <summary>
    /// 信息体对象
    /// </summary>
    public class InformationObject
    {
        private int InfomationObjectAddress;
        private InformationElement[,] infomationElements;

        public InformationObject(ByteBody data, int typeId, int numberOfSequenceElements)
        {
            this.InfomationObjectAddress = (data.GetByte() & 0xFF) + ((data.GetByte() & 0xFF) << 8) + ((data.GetByte() & 0xFF) << 16);
            switch (typeId)
            {
                //1 单点遥信
                case 1:
                    infomationElements = new InformationElement[numberOfSequenceElements,1];
                    for (int i = 0; i < numberOfSequenceElements; i++)
                    {
                        infomationElements[i,0] = new ESinglePointWithQuality(data);
                    }
                    break;
                //0D 浮点型遥测
                case 13:
                    infomationElements = new InformationElement[numberOfSequenceElements,2];
                    for (int i = 0; i < numberOfSequenceElements; i++)
                    {
                        infomationElements[i,0] = new EShortFloat(data);
                        infomationElements[i,1] = new EQuality(data);
                    }
                    break;
                //45 单点遥控返回确认数据读取
                case 45:
                    infomationElements = new InformationElement[numberOfSequenceElements, 1];
                    for (int i = 0; i < numberOfSequenceElements; i++)
                    {
                        infomationElements[i, 0] = new ESinglePointWithQuality(data);
                    }
                    break;
                //49标度化值返回确认数据获取
                case 49:
                    infomationElements = new InformationElement[numberOfSequenceElements, 2];
                    for (int i = 0; i < numberOfSequenceElements; i++)
                    {
                        infomationElements[i, 0] = new EScaled(data);
                        infomationElements[i, 1] = new EQuality(data);
                    }
                    break;
                //100总召
                case 100:
                    infomationElements = new InformationElement[,] { { new EQualifierOfInterrogation(data) } };
                    break;
                default:
                    throw new Exception("无法转换信息对象，类型标识：" + typeId);
            }
        }

        public int encode(byte[] buffer, int i)
        {
            int o_i = i;
            buffer[i++] = (byte)InfomationObjectAddress;
            buffer[i++] = (byte)(InfomationObjectAddress >> 8);
            buffer[i++] = (byte)(InfomationObjectAddress >> 16);
            foreach (var row in infomationElements)
            {
                //foreach (var item in row)
                //{
                //    i += item.Encode(buffer, i);
                //}
            }
            return i - o_i;
        }

        public int GetInformationObjectAddress()
        {
            return InfomationObjectAddress;
        }

        public InformationElement[,] GetInformationElements()
        {
            return infomationElements;
        }

    }
}
